package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        // 结算单的添加分2种情况
        // 1>客户事先已预约，然后到店消费产生结算，此时结算单中有appointment_id属性
        // 2>客户直接到店消费产生的结算，此时结算单中的appointment_id属性没值
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void delete(Long id) {
        statementMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public Statement selectByPrimaryKey(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal itemPrice, BigDecimal totalCount, BigDecimal totalAmount) {
        statementMapper.updateAmount(statementId,itemPrice,totalCount,totalAmount);
    }

    @Override
    public void payStatement(Long statementId) {
        //满足结算条件
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            // 如果当前的状态是已支付状态
            throw  new BusinessException("不能重复支付");
        }
        //修改支付状态
        statement.setStatus(Statement.STATUS_PAID);
        //修改 收款时间， 收款人
        statement.setPayTime(new Date());
        //收款人：当前登录用户
        statement.setPayeeId(ShiroUtils.getUserId());

        statementMapper.payStatement(statement);

    }

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = appointmentService.get(appointmentId);
        Statement statement = new Statement();
        if (Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())) { // 已到店
            //到店--创建结算单
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setAppointmentId(appointmentId);
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setInfo(appointment.getInfo());
            statement.setServiceType(appointment.getServiceType() + 0L);
            statement.setCreateTime(new Date());
            statement.setStatus(Statement.STATUS_CONSUME);
            statementMapper.insert(statement);
            //修改预约状态：结算单生成

            appointmentService.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
        }else{
            // 查询结算单
            statement = statementMapper.queryByAppointmentId(appointmentId);
        }
        //如果到店---创建结算单
        //如果结算单已经生成---不需要创建
        return statement;
    }


}
